package com.vcredit.creditcat.service;

import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.vcredit.creditcat.model.Log;
import com.vcredit.creditcat.persistence.LogMapper;
import com.vcredit.framework.bean.MsgBean;
import com.vcredit.framework.kafka.KafKaProcess;

@Component
public class LogService implements KafKaProcess {

	private static Logger logger = Logger.getLogger(LogService.class);

	@Autowired
	private LogMapper logMapper;

	@Override
	public void process(MsgBean msgBean) {

		logger.info("================================kafka log bean");

		if (msgBean != null) {
			Object className = msgBean.get("className");
			Object method = msgBean.get("methodName");
			Object param = msgBean.get("param");
			Object rid = msgBean.get("requestId");
			Object result = msgBean.get("result");
			Object level = msgBean.get("level");
			Object runTime = msgBean.get("runTime");
			Log log = new Log();
			if (className != null)
				log.setClassName(className.toString());
			if (method != null)
				log.setLogMethod(method.toString());
			if (param != null)
				log.setLogParam(param.toString());
			if (rid != null)
				log.setRequestId(rid.toString());
			if (result != null)
				log.setLogResult(result.toString());
			if (level != null)
				log.setLogLevel(level.toString());
			if (runTime != null)
				log.setRunTime(Long.valueOf(runTime.toString()));
			log.setLogTime(new Date());

			logMapper.insert(log);

		}
	}
}
